import datetime
import pytz

import coin.exchange.base.kr_rest.public_client_base as pubcb
import coin.exchange.ftx.kr_rest.native_public_client as npubc
from coin.exchange.ftx.kr_rest.public_client import FTXFeedParser
from coin.exchange.ftx_futures.kr_rest.futures_product import FTXFuturesProduct


class FTXFuturesPublicClient(pubcb.PublicClientBase):
  ProductType = FTXFuturesProduct

  def __init__(self):
    self.npubc = npubc.FTXNativePublicClient()

  def query_history_kline_impl(self, product, kline_period, start_time, end_time):
    native_symbol = product.native_symbol
    start_time = start_time.replace(tzinfo=pytz.UTC)
    end_time = end_time.replace(tzinfo=pytz.UTC)
    update = self.npubc.get_history_kline(
        native_symbol, kline_period, start_time, end_time)
    kline = FTXFeedParser.parse_native_kline(update.msg,
                                             product,
                                             kline_period,
                                             start_time,
                                             end_time)
    kline.market_type = 'Futures'
    update.msg = kline
    return update

  def query_level_book_impl(self, product):
    raise NotImplementedError("Ftx futures query level book Not Implemented")


if __name__ == "__main__":
  client = FTXFuturesPublicClient()
  start_time = datetime.datetime(2020, 11, 5).replace(tzinfo=pytz.UTC)
  end_time = datetime.datetime(2020, 11, 5, 1, 59, 59).replace(tzinfo=pytz.UTC)
  print(client.query_history_kline(
      FTXFuturesProduct.FromStr('BTC-1225'), 3600, start_time, end_time).msg)
